package de.openkanban.client.ui.adminpanel;

import com.smartgwt.client.types.AnimationEffect;
import com.smartgwt.client.types.BkgndRepeat;
import com.smartgwt.client.types.Positioning;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.widgets.AnimationCallback;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.ResizedEvent;
import com.smartgwt.client.widgets.events.ResizedHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VStack;

import de.openkanban.client.Main;
import de.openkanban.client.ui.board.BoardHeaderButton;
import de.openkanban.client.ui.presenter.LogOutRequestCallback;
import de.openkanban.client.ui.presenter.PreparedRequest;
import de.openkanban.client.util.RootPanel;

/**
 * GUI Element for the AdminPanel
 */
public class AdminPanel extends HLayout {

	private static final int PANEL_HEIGHT = 42;

	private IButton loginButton;
	private IButton logoutButton;
	private IButton signUpButton;
	private IButton userAccountButton;
	private IButton newBoardButton;

	private HLayout middleSpaceHLayout;
	private LoginForm loginForm;
	private SignUpForm signUpForm;

	private final VStack vsLoginVStack = new VStack();
	private final VStack vsSignUpVStack = new VStack();

	public AdminPanel() {

		// /////////////////////////////////////////////////////
		// Spacer
		// /////////////////////////////////////////////////////
		middleSpaceHLayout = new HLayout();
		middleSpaceHLayout.setWidth("*");

		// /////////////////////////////////////////////////////
		// Buttons
		// /////////////////////////////////////////////////////
		newBoardButton = new AdminButton("+ board");
		newBoardButton.setVisible(false);

		newBoardButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				RootPanel.main.getBoard().animateFade(0, new AnimationCallback() {
					@Override
					public void execute(boolean earlyFinish) {
						RootPanel.activateBoardEditMode(true);
						RootPanel.main.getBoard().animateFade(100, null, Main.DEFAULT_FADE_DURATION);
					}
				}, Main.DEFAULT_FADE_DURATION);
			}
		});

		userAccountButton = new AdminButton("user account");
		userAccountButton.setVisible(false);

		userAccountButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				UserAccountWindow userAccountWindow = new UserAccountWindow();
				userAccountWindow.animateShow(AnimationEffect.FADE, null, 500);
			}
		});

		signUpButton = new AdminButton("Sign Up");
		signUpButton.setOpacity(100);
		signUpButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (vsSignUpVStack.isVisible()) {
					vsSignUpVStack.animateHide(AnimationEffect.SLIDE, null, 250);
				} else {
					vsSignUpVStack.moveTo(getWidth() - 505, PANEL_HEIGHT);
					vsLoginVStack.animateHide(AnimationEffect.SLIDE, null, 250);
					vsSignUpVStack.animateShow(AnimationEffect.SLIDE, null, 250);
				}
			}
		});

		loginButton = new AdminButton("Login");
		loginButton.setIcon("login2.png");
		loginButton.setShowRollOver(true);
		loginButton.setShowDown(true);
		loginButton.setOpacity(100);
		loginButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (vsLoginVStack.isVisible()) {
					vsLoginVStack.animateHide(AnimationEffect.SLIDE, null, 250);
				} else {
					vsLoginVStack.moveTo(getWidth() - 326, PANEL_HEIGHT);
					vsSignUpVStack.animateHide(AnimationEffect.SLIDE, null, 250);
					vsLoginVStack.animateShow(AnimationEffect.SLIDE, null, 250);
				}
			}
		});

		logoutButton = new AdminButton("Logout");
		logoutButton.setVisible(false);
		logoutButton.setShowRollOver(true);
		logoutButton.setShowDown(true);
		logoutButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				PreparedRequest prepReq = new PreparedRequest("user", "logout");
				prepReq.sendAsyncRequest(new LogOutRequestCallback());
			}
		});

		// /////////////////////////////////////////////////////
		// DropDown Forms
		// /////////////////////////////////////////////////////
		signUpForm = new SignUpForm();

		IButton submitSignUp = new BoardHeaderButton("sign up");
		submitSignUp.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				signUpForm.submit();
			}
		});

		// vsSignUpVStack.setBackgroundColor("#12536e");
		vsSignUpVStack.setBackgroundColor("#eee");
		vsSignUpVStack.setShowShadow(true);
		vsSignUpVStack.setShadowSoftness(10);
		vsSignUpVStack.addMember(signUpForm);
		vsSignUpVStack.addMember(submitSignUp);
		vsSignUpVStack.setPosition(Positioning.ABSOLUTE);
		vsSignUpVStack.setPadding(2);
		vsSignUpVStack.setVisible(false);

		loginForm = new LoginForm();

		IButton submitLogin = new BoardHeaderButton("login");
		submitLogin.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				loginForm.submit();
			}
		});

		vsLoginVStack.setBackgroundColor("#eee");
		vsLoginVStack.setShowShadow(true);
		vsLoginVStack.setShadowSoftness(10);
		vsLoginVStack.addMember(loginForm);
		vsLoginVStack.addMember(submitLogin);

		vsLoginVStack.setPosition(Positioning.ABSOLUTE);
		vsLoginVStack.setPadding(2);
		vsLoginVStack.setVisible(false);

		// /////////////////////////////////////////////////////
		// AdminPanel Properties
		// /////////////////////////////////////////////////////
		setHeight(PANEL_HEIGHT);
		setBackgroundRepeat(BkgndRepeat.REPEAT_X);
		setBackgroundImage(Page.getAppImgDir() + "admin_panel_bg.png");

		addResizedHandler(new ResizedHandler() {
			@Override
			public void onResized(ResizedEvent event) {
				vsLoginVStack.moveTo(getWidth() - 326, PANEL_HEIGHT);
				vsSignUpVStack.moveTo(getWidth() - 505, PANEL_HEIGHT);
			}
		});

		addMember(userAccountButton);
		addMember(newBoardButton);

		addMember(middleSpaceHLayout);

		addMember(loginButton);
		addMember(logoutButton);
		addMember(signUpButton);
	}

	public IButton getLoginButton() {
		return loginButton;
	}

	public void setLoginButton(IButton loginButton) {
		this.loginButton = loginButton;
	}

	public IButton getSignUpButton() {
		return signUpButton;
	}

	public void setRegisterButton(IButton registerButton) {
		this.signUpButton = registerButton;
	}

	public HLayout getMiddleHLayout() {
		return middleSpaceHLayout;
	}

	public void setMiddleHLayout(HLayout middleHLayout) {
		this.middleSpaceHLayout = middleHLayout;
	}

	public IButton getLogoutButton() {
		return logoutButton;
	}

	public void setLogoutButton(IButton logoutButton) {
		this.logoutButton = logoutButton;
	}

	public VStack getVsLoginVStack() {
		return vsLoginVStack;
	}

	public LoginForm getLoginForm() {
		return loginForm;
	}

	public SignUpForm getSignUpForm() {
		return signUpForm;
	}

	public VStack getVsSignUpVStack() {
		return vsSignUpVStack;
	}

	public IButton getNewBoardButton() {
		return newBoardButton;
	}

	public IButton getUserAccountButton() {
		return userAccountButton;
	}

}